import { Component } from 'react';

import { Toast, Button, Group, TextareaField, CheckboxField, TextField } from 'saltui';

import DB from '../../../app/db';

import './PageHome.less';
const {
  Count
} = TextareaField;

export default class PageHome extends Component {


  constructor(props) {
    super(props);

    //关闭加载页的遮罩效果
    $(".loading-containter").hide();

    let id = this.props.params.id;
    this.state = {
      id: id,
      reason: '',
      messagePushData:[],
      remindContent:'',
      messagePushCheck:''
    };

    let t = this;
    let userId = document.getElementById("userHiddenId").value;
    var isHelpNew = 1;
    dd.util.domainStorage.getItem({ //帮助按钮
      name: 'meeting_helpNew_' + userId, // 存储信息的key值
      onSuccess: function (info) {
        var helpNew = info.value;
        if (typeof (helpNew) == 'undefined' || helpNew == '') {
          isHelpNew = 2;
        }
        //初始化按钮
        t.initButton(isHelpNew);
      },
      onFail: function (err) {
        //alert(JSON.stringify(err));
      }
    });
  }

  //初始化按钮
  initButton(isHelpNew) {
    //设置帮助
    /*dd.biz.navigation.setIcon({
        showIcon : true,//是否显示icon
        iconIndex : isHelpNew,//显示的iconIndex,如上图
        onSuccess : function(result) {
            location.hash='help/help_index';
        },
        onFail : function(err) {
        //jsapi调用失败将会回调此函数
        }
    });*/
  }

  componentDidMount() {
    DB.SomeModuleAPI.getCreatedbyNameAndApplyInfo({id:this.state.id}).then((content)=>{
      let messagePushData=[];
      let messagePushSW=content.messagePushSW;
      let remindContent=content.createdName+"的"+content.name+"会议已被取消";
      let messagePushCheck="";
      if (messagePushSW.indexOf("mobile") > -1) {
        let moduleLocation = messagePushSW.indexOf("mobile");
        let type = messagePushSW.substring(moduleLocation + 7, moduleLocation + 8);
        if (type == "2") {
          messagePushData.push({ value: 'mobile', text: '移动端', checked: true });
          messagePushCheck+="mobile";
        } else if (type == "1") {
          messagePushData.push({ value: 'mobile', text: '移动端' });
        }
      }
      if (messagePushSW.indexOf("sms") > -1) {
        let moduleLocation = messagePushSW.indexOf("sms");
        let type = messagePushSW.substring(moduleLocation + 4, moduleLocation + 5);
        if (type == "2") {
          messagePushData.push({ value: 'sms', text: '短信', checked: true });
          messagePushCheck+=",sms";
        } else if (type == "1") {
          messagePushData.push({ value: 'sms', text: '短信' });
        }
  
      }
      //放在下面组件只需刷新一次
      this.setState({remindContent:remindContent,messagePushCheck:messagePushCheck,messagePushData:messagePushData});
    }).catch((error) => {
      alert("111:"+JSON.stringify(error));
    });    
    
  }

  componentDidUpdate() {
    var thirdMenuType = getLoginUserInfo().thirdMenuType;
    if (thirdMenuType == 'DING') {
      dd.biz.navigation.setTitle({
        title: "取消原因",//控制标题文本，空字符串表示显示默认文本
        onSuccess: function (result) { },
        onFail: function (err) { }
      });
    } else if (thirdMenuType == 'MH') {
      !!emp && emp.window.setTitle({
        title: {
          text: "取消原因",
          icon: '',
          actionmenu: {}
        },
      });
    }
  }

  //文本框输入
  handleTextChange(field, newValue, size) {
    if (typeof(size)!="undefined" && newValue.length <= size) {
      this.setState({
        [field]: newValue,
      });
    }else{
      this.setState({
        [field]: newValue,
      });
    }
  }

  submit() {
    if (this.state.reason.trim() == '') {
      Toast.show({
        type: 'error',
        content: '请输入取消原因',
        onHide: function () {
        }
      });
      return;
    }
    if (this.state.reason != null && isEmojiCharacter(this.state.reason)) {
      Toast.show({
        type: 'error',
        content: '取消原因不支持表情输入',
        onHide: function () { }
      });
      return;
    }
    if (this.state.remindContent.trim() == '') {
      Toast.show({
        type: 'error',
        content: '请输入提醒内容',
        onHide: function () { }
      });
      return;
    }
    
    let param = {
      id: this.state.id,
      reason: this.state.reason,
      remindContent:this.state.remindContent,
      messagePushCheck:this.state.messagePushCheck,
      status: '3'
    }

    DB.SomeModuleAPI.editApplyRecordInfo(param)
      .then((content) => {

        Toast.show({
          type: 'success',
          content: '保存成功'
        });

        //返回至列表页面
        setTimeout(function () {
          history.back();
        }, 2000);
      }).catch((error) => {

        if (typeof (error.errorMsg) == 'undefined') {
          Toast.show({
            type: 'error',
            content: '保存出错'
          });
        } else {
          Toast.show({
            type: 'error',
            content: error.errorMsg
          });
        }
      });

  }

  cancel() {
    history.back();
  }

  changeMessagePush(data){
    let messagePush = "";
    for(let i = 0; i < data.length; i++){
      if(i == data.length-1) messagePush += data[i].value;
      else messagePush += data[i].value + ',';
    }
    this.setState({
      messagePushCheck:messagePush
    });
    //alert(JSON.stringify(data));
  }

  render() {
    let t = this;
    return (
      <div>
        <div>
          <Group.List >
            <TextareaField
              required
              label="取消原因" minRows={6} maxRows={10}
              placeholder="请输入取消原因"
              value={t.state.reason}
              onChange={(value) => { t.handleTextChange('reason', value, 200); }}>
              <Count total={200} length={t.state.reason.length} />
            </TextareaField>
          </Group.List>
          <Group.List className={t.state.messagePushSW != '' ? '' : 't-DN'}>
            <CheckboxField
              data={t.state.messagePushData}
              mode="slot"
              label="消息推送"
              onChange={t.changeMessagePush.bind(this)}
              required={false}
            />
          </Group.List>
          <Group.List >
          <TextField
            required
            label="提醒内容"
            placeholder="请输入提醒内容"
            value={t.state.remindContent}
            readOnly={false}
            onChange={(value) => { t.handleTextChange('remindContent', value); }}>
          </TextField>
        </Group.List>
        </div>

        <div >
          <Button.Group >
            <Button type="primary" display="inline" onClick={this.submit.bind(this)}>提交</Button>
            <Button type="secondary" display="inline" onClick={this.cancel.bind(this)}>取消</Button>
          </Button.Group>
        </div>

      </div>);
  }
}


